package me.zk.practice1.hw;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class HWBJ63 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String str = scanner.next();

        int len = scanner.nextInt();

        //统计字符
        Map<Character,Integer> map1 = new HashMap<>();

        getOrReChar(str,map1,true);
        Map<Character,Integer> map2 = new HashMap<>();
        int n = str.length()/len;
        int left = (int)Math.pow(10,n-1);
        int r = left;

        while (r <= 1000) {
            if (r-left == len) {
                if (isMatch(map1,map2)) {
                    break;
                }
                //减去left的字符
                getOrReChar(String.valueOf(left),map2,false);
                //右移动
                left++;
            }
            getOrReChar(String.valueOf(r),map2,true);
            //统计r的字符
            r++;
        }
        System.out.println(left);
    }


    public static void getOrReChar(String str,Map<Character,Integer> map,boolean add) {
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            map.put(c,map.getOrDefault(c,0)+ (add?1:-1));
        }
    }

    public static boolean isMatch(Map<Character,Integer> map1,Map<Character,Integer> map2) {
        for (Character c : map1.keySet()) {
            if (map2.containsKey(c)&&map2.get(c)!=map1.get(c)) {
                return false;
            }
        }
        return true;
    }
}
